
package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.common.constant.LgbConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.app.entity.Filemgr;
import org.springblade.modules.app.service.IFilemgrService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件管理表 控制器
 *
 * @author BladeX
 * @since 2020-08-22
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-app/filemgr")
@Api(value = "文件管理表", tags = "文件管理表接口")
public class FilemgrController extends BladeController {

    private final IFilemgrService filemgrService;



    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入filemgr")
    public R<Filemgr> detail(Filemgr filemgr) {
        Filemgr detail = filemgrService.getOne(Condition.getQueryWrapper(filemgr));
        return R.data(detail);
    }

    /**
     * 分页 文件管理表
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入filemgr")
    public R<IPage<Filemgr>> list(Filemgr filemgr, Query query) {
        IPage<Filemgr> pages = filemgrService.page(Condition.getPage(query), Condition.getQueryWrapper(filemgr));
        return R.data(pages);
    }

    /**
     * 自定义分页 文件管理表
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入filemgr")
    public R<IPage<Filemgr>> page(Page page, Filemgr filemgr) {
        LambdaQueryWrapper<Filemgr> queryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isEmpty(filemgr.getOriginalname())) {
            queryWrapper.and(wrapper -> wrapper.like(Filemgr::getOriginalname, filemgr.getOriginalname()));
        }
        IPage<Filemgr> pages = filemgrService.page(page, queryWrapper);
        return R.data(pages);
    }

    @PostMapping("/upload")
    public R save(@RequestParam(value = "file") MultipartFile multipartFile) {
        ///  abcdefg.doc
        String fileFullName = multipartFile.getOriginalFilename();
        //// abcdefg
        String fileSimpleName = fileFullName.substring(0, fileFullName.lastIndexOf("."));
        /// .doc
        String fileSuffix = fileFullName.substring(fileFullName.lastIndexOf("."), fileFullName.length());


        Long fileid = IdWorker.getId();

        String fileDigestName = fileid + fileSuffix;


        //linux操作系统
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        String datePath = "/opt/" + year + "/" + month + "/";
        File filepath = new File(datePath);
        if (!filepath.exists() && !filepath.isDirectory()) {
            System.out.println("//不存在创建路径//");
            filepath.mkdirs();
        }

        File file = new File(datePath + fileDigestName);
        try {
            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String handlePath = year + "/" + month + "/";

        Filemgr filemgr = new Filemgr();
        filemgr.setOriginalname(fileFullName);
        filemgr.setDigestname(fileDigestName);
        //动态常量 动态域名/IP 环境
        filemgr.setFileurl(LgbConstant.DYNAMIC_UPLOAD_PATH + handlePath + fileDigestName);
        filemgr.setCtime(new Date());
        filemgrService.save(filemgr);
        Map<String, String> map = new HashMap<>();
        map.put("name", fileDigestName);
        map.put("url", filemgr.getFileurl());
        return R.data(map);
    }

    /**
     * 新增 文件管理表
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入filemgr")
    public R save(@RequestBody Filemgr filemgr) {
        return R.status(filemgrService.save(filemgr));
    }

    /**
     * 修改 文件管理表
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入filemgr")
    public R update(@RequestBody Filemgr filemgr) {
        return R.status(filemgrService.updateById(filemgr));
    }

    /**
     * 新增或修改 文件管理表
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入filemgr")
    public R submit(@RequestBody Filemgr filemgr) {
        return R.status(filemgrService.saveOrUpdate(filemgr));
    }


    /**
     * 删除 文件管理表
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(filemgrService.removeByIds(Func.toLongList(ids)));
    }


}
